package com.cskaoyan.order.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.order.constant.OrderConstants;
import com.cskaoyan.order.dto.CreateOrderRequest;
import com.cskaoyan.order.dto.CreateOrderResponse;
import com.cskaoyan.order.dto.DeleteOrderRequest;
import com.cskaoyan.order.dto.DeleteOrderResponse;
import com.cskaoyan.order.form.CreateOrder;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.CancelOrderForm;
import com.cskaoyan.order.form.PageInfo;
import com.cskaoyan.order.service.OrderQueryService;
import com.cskaoyan.order.service.OrderUpdateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: hogan
 * @Date: 2022/06/18/21:29
 * @Description: 订单表
 * @author wjs
 * @ClassName OrderController
 * @Description 创建日期: 2022/6/18 14:33
 */
@RestController
@RequestMapping("/shopping")
public class OrderController {

    @Autowired
    private OrderCoreService orderCoreService;

    @Autowired
    private OrderQueryService orderQueryService;

    @Autowired
    OrderUpdateService orderUpdateService;

    @PostMapping("/order")
    public ResponseData createOrder(@RequestBody CreateOrder order, HttpServletRequest request){
        String userInfo = (String) request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        order.setUserId(uid);
        CreateOrderRequest createOrderRequest = BeanUtil.copyProperties(order, CreateOrderRequest.class);
        CreateOrderResponse createOrderResponse = orderCoreService.createOrder(createOrderRequest);
        if (createOrderResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            return new ResponseUtil().setData(createOrderResponse.getOrderId(), OrderConstants.ORDER_CREATE_SUCCESS);
        }
        return new ResponseUtil().setErrorMsg(createOrderResponse.getMsg());
    }

    @DeleteMapping("/order/{id}")
    public ResponseData deleteOrder(@PathVariable("id") Long id){
        DeleteOrderRequest deleteOrderRequest = new DeleteOrderRequest();
        deleteOrderRequest.setOrderId(id.toString());
        DeleteOrderResponse deleteOrderResponse = orderQueryService.deleteOrder(deleteOrderRequest);
        if ("200".equals(deleteOrderResponse.getCode())){
            return new ResponseUtil().setData(OrderConstants.ORDER_CREATE_SUCCESS);
        }
        return new ResponseUtil().setErrorMsg(OrderConstants.ORDER_DELETE_FAIL);
    }




    @GetMapping("/order")
    public ResponseData orderList(PageInfo pageInfo, HttpServletRequest request){
        //从请求头获取用户信息
        String userInfo = (String) request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        //传给service层的参数封装为指定格式对象
        OrderListRequest orderListRequest = new OrderListRequest();
        orderListRequest.setUserId(uid);
        orderListRequest.setPage(pageInfo.getPage());
        orderListRequest.setSize(pageInfo.getSize());
        orderListRequest.setSort(pageInfo.getSort());

        //调用service
        OrderWithDetailResponse orderWithDetailResponse = orderQueryService.orderList(orderListRequest);

        //判断返回结果，根据结果进行响应处理
        if (orderWithDetailResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            OrderWithDetailResponse response = new OrderWithDetailResponse();
            response.setData(orderWithDetailResponse.getData());
            response.setTotal(orderWithDetailResponse.getTotal());
            return new ResponseUtil().setData(response);
        }

        return new ResponseUtil().setErrorMsg(orderWithDetailResponse.getMsg());
    }

    @GetMapping("/order/{orderId}")
    public ResponseData orderDetail(@PathVariable("orderId") String orderId){
        OrderDetailRequest request = new OrderDetailRequest();
        request.setOrderId(orderId);
        OrderDetailResponse response = orderQueryService.orderDetail(request);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            return new ResponseUtil().setData(response.getOrderDetail());
        }

        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @PostMapping("/cancelOrder")
    public ResponseData orderCancel(@RequestBody CancelOrderForm cancelOrderForm){
        CancelOrderRequest request = new CancelOrderRequest();
        request.setOrderId(cancelOrderForm.getOrderId());
        CancelOrderResponse response = orderUpdateService.orderCancel(request);

        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            return new ResponseUtil().setData(null);
        }

        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

}
